package com.ship.dispatch.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.core.utils.BeanCopyUtils;
import com.ship.dispatch.model.SpArrange;
import com.ship.dispatch.model.Vo.CommonDataVo;
import com.ship.dispatch.model.Vo.SpArrangeSourceRateVo;
import com.ship.dispatch.model.Vo.SpArrangeStatisticsVo;
import com.ship.dispatch.model.Vo.SpArrangeVo;
import com.ship.dispatch.model.dto.SpArrangeChartDto;
import com.ship.dispatch.model.dto.SpArrangeDto;
import com.ship.dispatch.model.dto.SpArrangePageDto;
import com.ship.dispatch.model.dto.SpArrangeStateUpdateDto;
import com.ship.dispatch.service.SpArrangeService;
import com.ship.dispatch.mapper.SpArrangeMapper;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author zhangyh
* @description 针对表【sp_arrange(货船排布)】的数据库操作Service实现
* @createDate 2023-11-03 10:17:22
*/
@Service
public class SpArrangeServiceImpl extends ServiceImpl<SpArrangeMapper, SpArrange>
    implements SpArrangeService{

    @Override
    public JsonResult<SpArrangeVo> detail(Long id) {
        SpArrange spArrange = this.baseMapper.selectById(id);
        if(spArrange == null){
            return JsonResult.failed("排布信息异常！");
        }
        SpArrangeVo resultData = new SpArrangeVo();
        BeanCopyUtils.copy(spArrange,resultData);
        return JsonResult.success(resultData);
    }

    @Override
    public JsonResult<Page<SpArrangeVo>> getPage(Page page, SpArrangePageDto pageDto) {
        Page<SpArrangeVo> resultPage = this.baseMapper.getPage(page,pageDto);
        return JsonResult.success(resultPage);
    }

    @Override
    public JsonResult<Long> add(SpArrangeDto dto) {
        SpArrange spArrange = new SpArrange();
        BeanCopyUtils.copy(dto,spArrange);
        this.baseMapper.insert(spArrange);
        return JsonResult.success(spArrange.getId());
    }

    @Override
    public JsonResult<Long> update(SpArrangeDto dto) {
        SpArrange spArrange = this.baseMapper.selectById(dto.getId());
        if(spArrange == null){
            return JsonResult.failed("排布信息异常！");
        }
        BeanCopyUtils.copy(dto,spArrange);
        this.baseMapper.updateById(spArrange);
        return JsonResult.success();
    }

    @Override
    public JsonResult delete(Long id) {
        SpArrange spArrange = this.baseMapper.selectById(id);
        if(spArrange == null){
            return JsonResult.failed("排布信息异常！");
        }
        this.baseMapper.deleteById(id);
        return JsonResult.success();
    }

    @Override
    public JsonResult<Long> stateUpdate(SpArrangeStateUpdateDto dto) {
        SpArrange spArrange = this.baseMapper.selectById(dto.getId());
        if(spArrange == null){
            return JsonResult.failed("排布信息异常！");
        }
        if(dto.getState().intValue() == 1){
            //下发
            if(spArrange.getState().intValue() != 0){
                return JsonResult.failed("排布状态异常，无法下发！");
            }
        }else if(dto.getState().intValue() == 4){
            //终止
            if(spArrange.getState().intValue() == 3){
                return JsonResult.failed("已完成的船货排布不能终止！");
            }
        }else{
            return JsonResult.failed("未知的状态！");
        }
        spArrange.setState(dto.getState());
        this.baseMapper.updateById(spArrange);

        return JsonResult.success();
    }

    @Override
    public JsonResult<SpArrangeStatisticsVo> topStatistics() {
        SpArrangeStatisticsVo resultData = this.baseMapper.topStatistics();
        if (resultData != null) {
            //方案平均优化次数和实际优化次数
            List<CommonDataVo> disCountList = this.baseMapper.getDisCount();
            int allCount = 0;
            if (disCountList.size() > 0) {
                int schemeCount = 0;
                for (CommonDataVo commonDataVo : disCountList) {
                    allCount += Integer.parseInt(commonDataVo.getDataValue1());
                    schemeCount++;
                }
                BigDecimal allCountDec = new BigDecimal(String.valueOf(allCount));
                BigDecimal schemeCountDec = new BigDecimal(String.valueOf(schemeCount));
                resultData.setSchemeOperateCount(allCountDec);
                resultData.setSchemeOperateCountAvg(allCountDec.divide(schemeCountDec, 1, RoundingMode.CEILING));
            }
            //资源方占比
            List<SpArrangeSourceRateVo> sourceCountList = this.baseMapper.getSourceCount();
            if (sourceCountList.size() > 0 && allCount > 0) {
                List<SpArrangeSourceRateVo> resourceSideRate = new LinkedList<>();
                for (SpArrangeSourceRateVo sourceRateVo : sourceCountList) {
                    sourceRateVo.setSourceRate(sourceRateVo.getSourceCount().divide(
                            new BigDecimal(String.valueOf(allCount)), 2, RoundingMode.CEILING));
                    resourceSideRate.add(sourceRateVo);
                }
                resultData.setResourceSideRate(resourceSideRate);
            }
        }
        return JsonResult.success(resultData);
    }

    @Override
    public JsonResult getChart(SpArrangeChartDto dto) {
        //根据方案编号来获取每个方案最新的一条数据集
        List<SpArrangeVo> arrangeVos = this.baseMapper.getNewestArrangeListBySchemeNo();
        if(arrangeVos.size() == 0){
            JsonResult.failed("暂无船货排布信息！");
        }
        JSONObject resultData = new JSONObject();
        //船舶排布
        Map<String,List<SpArrangeVo>> boatMap = arrangeVos.stream().collect(Collectors.groupingBy(SpArrangeVo::getNameOfVessel));
        JSONArray boatArray = new JSONArray();
        for (String boatName : boatMap.keySet()) {
            JSONObject boatDetailJson = new JSONObject();
            boatDetailJson.put("shipName",boatName);
            boatDetailJson.put("details",boatMap.get(boatName));
            boatArray.add(boatDetailJson);
        }
        resultData.put("shipNames",boatArray);

        //资源排布
        Map<String,List<SpArrangeVo>> goodsMap = arrangeVos.stream().collect(Collectors.groupingBy(SpArrangeVo::getGoodsNumber));
        JSONArray goodsArray = new JSONArray();
        for (String goodsName : goodsMap.keySet()) {
            JSONObject goodsDetailJson = new JSONObject();
            goodsDetailJson.put("goodsName",goodsName);
            goodsDetailJson.put("details",goodsMap.get(goodsName));
            goodsArray.add(goodsDetailJson);
        }
        resultData.put("goodsNames",goodsArray);
        //资源方排布
        Map<String,List<SpArrangeVo>> sourceSideMap = arrangeVos.stream().collect(Collectors.groupingBy(SpArrangeVo::getResourceSide));
        JSONArray sourceSideArray = new JSONArray();
        for (String sourceSideName : sourceSideMap.keySet()) {
            JSONObject sourceSideDetailJson = new JSONObject();
            sourceSideDetailJson.put("sellerName",sourceSideName);
            sourceSideDetailJson.put("details",sourceSideMap.get(sourceSideName));
            sourceSideArray.add(sourceSideDetailJson);
        }
        resultData.put("sellerNames",sourceSideArray);
        return JsonResult.success(resultData);
    }

}




